package cn.umakr.web;

import cn.umakr.entity.Dept;
import cn.umakr.entity.User;
import cn.umakr.service.DeptPageService;
import cn.umakr.service.DeptService;
import cn.umakr.service.UserService;
import cn.umakr.util.PageUtil;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangbo on 2017/7/19.
 */
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserService();
    private DeptService deptService = new DeptService();
    private DeptPageService deptPageService = new DeptPageService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置request与response的编码
        response.setContentType("text/html");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        //获取前台表单发过来的数据信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //验证登录用户信息
        User user = userService.validate(username, password);
        if (user != null) {
            request.getSession().setAttribute("user", user);
            //获取部门列表信息
            List<Dept> depts = deptService.findAllDepts();
            request.getSession().removeAttribute("depts");
            request.getSession().setAttribute("depts", depts);
            Integer currentPage = 1;
            Integer pageSize = PageUtil.getPageSize();
            //获取部门数据总条数
            Integer totalRecord = deptService.getCount();
            //获取总页数
            Integer totalPage = deptPageService.getMaxPage();
            //分页获取部门列表数据
//            List<Dept> depts = deptService.pageDepts(currentPage, pageSize);
//            request.getSession().setAttribute("currentPage", currentPage);
//            request.getSession().setAttribute("totalRecord", totalRecord);
//            request.getSession().setAttribute("totalPage", totalPage);
//            request.getSession().removeAttribute("depts");
//            request.getSession().setAttribute("depts", depts);
            String deptsJson = deptService.pageDeptsJson(currentPage, pageSize);
            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("depts", deptsJson);
            pageMap.put("currentPage", currentPage);
            pageMap.put("totalRecord", totalRecord);
            pageMap.put("totalPage", totalPage);
            Gson gson = new Gson();
            out.print(gson.toJson(pageMap));
            out.flush();
            out.close();
//            request.getRequestDispatcher("/pages/main.jsp").forward(request, response);
        } else {
//            request.setAttribute("msg", "用户名或密码错误，请重新登录！");
//            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }

}
